package avicit.im.lantop.basic.lexiconManager.service.impl;

import avicit.im.lantop.basic.lexiconManager.service.LantopLexiconService;
import avicit.im.lantop.basic.themewordmanager.domain.LantopThemeWord;
import avicit.im.lantop.basic.useconfig.domain.LantopUseSet;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.PojoUtil;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.domain.BeanBase;
import avicit.platform6.core.exception.DaoException;
import avicit.platform6.core.jdbc.JdbcAvicit;
import avicit.platform6.core.jdbc.Page;
import avicit.platform6.core.properties.PlatformConstant.OpResult;
import avicit.platform6.core.properties.PlatformConstant.OpType;
import avicit.platform6.core.service.ServiceBase;
import avicit.platform6.core.session.SessionHelper;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>金航数码科技有限责任公司</p>
 * <p>作者：胡光昌</p>
 * <p>邮箱：huguangc@avicit.com</p>
 * <p>创建时间： 2014-10-13 上午9:31:00 </p>
 * <p>类说明：主题词模块接口实现类</p>
 * <p>修改记录： </p>
 */
@Service
public class LantopLexiconServiceImpl extends ServiceBase implements LantopLexiconService {

	@Autowired
	private JdbcAvicit jdbcAvicit;

	/**
	 * 删除方法
	 */
	@Override
	public void delete(BeanBase bean) throws DaoException {
		LantopThemeWord lantopThemeWord = (LantopThemeWord) bean;
		if (lantopThemeWord == null) {
			throw new DaoException(new StringBuffer(bean.toString()).append(" 对象转换为LantopThemeWord对象出错!").toString());
		}
		hibernateDao.delete(lantopThemeWord);
		SysLogUtil.log(lantopThemeWord, null, OpType.delete, OpResult.success);

	}

	/**
	 * 新增方法
	 */
	@Override
	public void insert(BeanBase bean) throws DaoException {
		LantopThemeWord lantopThemeWord = (LantopThemeWord) bean;
		if (lantopThemeWord == null) {
			throw new DaoException(bean.toString() + "对象转换为LantopThemeWord对象出错！");
		}
		lantopThemeWord.setId(ComUtil.getId());
		PojoUtil.setSysProperties(lantopThemeWord, OpType.insert);
		hibernateDao.save(lantopThemeWord);
		SysLogUtil.log(lantopThemeWord, null, OpType.insert, OpResult.success);

	}

	/**
	 * 更新方法
	 */
	@Override
	public void update(BeanBase bean) throws DaoException {
		LantopThemeWord theme = (LantopThemeWord) bean;
		if (theme == null) {
			throw new DaoException(bean.toString() + "对象转换为LantopThemeWord对象出错！");
		}
		LantopThemeWord oldBean = getLantopThemeWordById(theme.getId());
		this.hibernateDao.evict(oldBean);
		LantopThemeWord newBean = new LantopThemeWord();
		PojoUtil.copyProperties(newBean, oldBean, true);
		PojoUtil.copyProperties(newBean, theme, true);
		SysLogUtil.log(newBean, oldBean, OpType.update, OpResult.success);
		PojoUtil.setSysProperties(newBean, OpType.update);
		this.hibernateDao.update(newBean);
	}

	/**
	 * 获取对象
	 * @param id
	 * @return
	 */
	private LantopThemeWord getLantopThemeWordById(String id) {
		LantopThemeWord theme = this.hibernateDao.get(LantopThemeWord.class, id);
		return theme;
	}

	/**
	 * 查询初始化数据
	 * 
	 */
	@Override
	public Page<LantopThemeWord> getPageList(Page<LantopThemeWord> page, Map<String, Object> parameter) {
		StringBuffer sbSql = new StringBuffer();
		sbSql.append("SELECT * FROM LANTOP_THEME_WORD LTW ");
		String orderBy = "FORMAL_THEME_WORD ";
		Map<String, Object> sqlMap = jdbcAvicit.buildDynamicSql(sbSql.toString(), parameter, "LTW");
		String sqlResult = sqlMap.get("sqlResult").toString();
		sqlResult += " ORDER BY " + orderBy;
		sqlMap.put("sqlResult", sqlResult);
		jdbcAvicit.getDatasByPage(page, sqlMap, LantopThemeWord.class);
		return page;
	}

	/**
	 * 更新数据
	 */
	@Override
	public void updateData(LantopThemeWord themeWord) {
		this.update(themeWord);
	}

	/**
	 * 保存数据
	 */
	@Override
	public void insertData(LantopThemeWord themeWord) {
		themeWord.setOrgId(ThreadContextHelper.getOrgId());
		themeWord.setDeptId(ThreadContextHelper.getDeptId());
		this.insert(themeWord);
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 * @throws
	 */
	@Override
	public void deleteThemeWord(String ids) {
		if (ids.length() > 0) {
			String[] idsArray = ids.split(",");
			for (int i = 0; i < idsArray.length; i++) {
				LantopThemeWord lantopThemeWord = this.hibernateDao.get(LantopThemeWord.class, idsArray[i]);
				this.hibernateDao.delete(lantopThemeWord);
				SysLogUtil.log(lantopThemeWord, null, OpType.delete, OpResult.success);
			}
		}
	}

	/**
	 * 查询是否有重复的正式关键词
	 * 
	 * @param code,id,orgId
	 * @return int
	 * @throws
	 */
	@Override
	public int getCount(String orgId,String code, String id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ID FROM LANTOP_THEME_WORD WHERE FORMAL_THEME_WORD = '" + code + "' ");
		sql.append(" AND ORG_ID = '"+orgId+"'");
		if (!id.equals("")) {
			sql.append(" AND ID != '" + id + "'");
		}
		List<?> list = jdbcAvicit.getJdbcTemplate().queryForList(sql.toString());
		return list.size();
	}

	/**
	 * 获取初始化配置数据
	 * @param sysId,orgId,deptId
	 * @return String
	 * @throws 
	 */
	@Override
	public String getConfigInfo(String sysId, String orgId, String deptId) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT KEY_VALUE FROM LANTOP_USE_SET WHERE SYS_ID = '");
		sql.append(sysId);
		sql.append("' AND ORG_ID = '");
		sql.append(orgId);
		/*
		 * sql.append("' AND DEPT_ID = '"); sql.append(deptId);
		 */
		sql.append("' AND KEY_WORD = 'IS_USE_MARK'");
		String isUseMark = "2";
		List<Map<String, Object>> keyWordList = new ArrayList<Map<String, Object>>();
		keyWordList = jdbcAvicit.queryForMap(sql.toString(), null);
		if (keyWordList != null && keyWordList.size() > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map = keyWordList.get(0);
			isUseMark = String.valueOf(map.get("KEY_VALUE").toString());
		}
		return isUseMark;
	}

	/**
	 * 修改自动索引为未启用
	 */
	@Override
	public void updateIsUseWord(String orgId,String sysId, String isUseWord) {
		String id = this.findAutoThemeSearch(sysId, isUseWord,orgId);
		LantopUseSet oldBean = this.hibernateDao.get(LantopUseSet.class, id);
		this.hibernateDao.evict(oldBean);
		LantopUseSet newBean = new LantopUseSet();
		PojoUtil.copyProperties(newBean, oldBean, true);
		newBean.setKeyValue("2");
		SysLogUtil.log(newBean, oldBean, OpType.update, OpResult.success);
		PojoUtil.setSysProperties(newBean, OpType.update);
		this.hibernateDao.update(newBean);
	}

	/**
	 * 查询全局自动索引数据
	 */
	@Override
	public String findAutoThemeSearch(String sysId, String isUseWord,String orgId) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ID FROM LANTOP_USE_SET WHERE SYS_ID = '");
		sql.append(sysId);
		sql.append("' AND KEY_WORD = '");
		sql.append(isUseWord);
		sql.append("'");
		sql.append(" AND ORG_ID = '"+orgId+"'");
		String id = "";
		List<Map<String, Object>> idList = new ArrayList<Map<String, Object>>();
		idList = jdbcAvicit.queryForMap(sql.toString(), null);
		if (idList != null && idList.size() > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map = idList.get(0);
			id = String.valueOf(map.get("ID").toString());
		}
		return id;
	}

	/**
	 * 修改全局配置为启动
	 */
	@Override
	public void updateAutoThemeWord(String id) {
		LantopUseSet oldBean = this.hibernateDao.get(LantopUseSet.class, id);
		this.hibernateDao.evict(oldBean);
		LantopUseSet newBean = new LantopUseSet();
		PojoUtil.copyProperties(newBean, oldBean, true);
		newBean.setKeyValue("1");
		SysLogUtil.log(newBean, oldBean, OpType.update, OpResult.success);
		PojoUtil.setSysProperties(newBean, OpType.update);
		this.hibernateDao.update(newBean);
	}
}
